package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.vo.TeacherQuery;
import com.atguigu.eduservice.service.EduTeacherService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.swing.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-02-24
 */
@Api(description="讲师管理")  /*swagger上面的提示*/
@RestController
@RequestMapping("/eduservice/teacher")
//原来需要在Controller上面加上@CrossOrigin解决跨域问题，现在gateway已经处理了/*解决跨域问题*/
public class EduTeacherController {

    //访问地址： http://localhost:8001/eduservice/teacher/findAll
    //把service注入
    @Autowired
    private EduTeacherService teacherService;


    @ApiOperation(value = "讲师修改功能")
    @PostMapping("/updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher eduTeacher){
        boolean flag = teacherService.updateById(eduTeacher);
        if(flag){
            return R.ok();
        }else {
            return R.error();
        }
    }

    @ApiOperation(value = "添加讲师的方法")
    @PostMapping("/addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduTeacher){
        boolean flag = teacherService.save(eduTeacher);
        if(flag){
            return R.ok();
        }else {
            return R.error();
        }
    }


    @ApiOperation(value = "条件分页查询教师信息")
    @PostMapping("getTeacherCondition/{current}/{limit}")
    public R getTeacherCondition(@PathVariable long current,
                                 @PathVariable long limit,
                                 @RequestBody TeacherQuery teacherQuery){
        /*条件分页查询讲师信息*/
        return teacherService.getTeacherCondition(current,limit,teacherQuery);
    }

    @ApiOperation(value = "根据讲师id进行查询")
    @GetMapping("/getTeacher/{id}")
    public R getTeacher(@PathVariable("id") Long id){
        EduTeacher teacher = teacherService.getById(id);
        if(teacher!=null){
            return R.ok().data("teacher",teacher);
        }else {
            return R.error();
        }
    }

    /*2.逻辑删除讲师*/
    /* @ApiParam(name = "id",value = "讲师ID",required = true) 是swagger的相关注解 */
    @ApiOperation(value = "根据讲师id逻辑删除讲师")
    @DeleteMapping("/deleteById/{id}")
    public R deleteById(@ApiParam(name = "id",value="讲师ID",required = true)
                        @PathVariable("id") String id){
        boolean flag = teacherService.removeById(id);
        if(flag==true){
            return R.ok();
        }
        return R.error();
    }

    @ApiOperation(value = "发布新课程里面的查询所有讲师的接口")
    @GetMapping("/findAll")
    public R findAll(){
        return teacherService.findAll();
    }


//    ------
/*前台的相关方法*/
    @ApiOperation(value = "前台分页查询老师的信息")
    @GetMapping("/frontTeacherPage/{current}/{limit}")
    public R frontTeacherPage(@PathVariable long current,
                              @PathVariable long limit){
        return teacherService.frontTeacherPage(current,limit);
    }

    @ApiOperation(value = "根据老师的id查询老师的详细信息+老师的主讲课程")
    @GetMapping("/selectTeacherById/{id}")
    public R selectTeacherById(@PathVariable("id") long id){
        return teacherService.selectTeacherById(id);
    }

}

